package com.cs2340.taskplusplus;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AccountDatabase
{
	public static void buildDatabase(Context c)
	{
		SQLiteDatabase db = c.openOrCreateDatabase("data", 0, null);
		db.close();
		db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db",null);
        try {
        	db.query("Accounts", null, null, null, null, null, null);
        }
        catch(Exception e)
        {
        	db.execSQL("CREATE TABLE Accounts (Name char(50), Password char(50), Email char(50), Real_Name char(50));");
        }
        
    	Cursor check = db.query("Accounts", new String[] {"Name"}, "Name='default'", null, null, null, null);
    	if (check.getCount()==0)
			new Account("default", "default", "default", "default");
		else
			new Account("default");
    	check.close();
    	db.close();
	}
	
	public static void createAccount(Account a)
	{
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db",null);
		db.execSQL("INSERT INTO Accounts VALUES ('"+Account.username+"', '" + a.getPassword() + "', '" + a.getRealname() + "', '" + a.getEmail() + "');");
		db.close();
	}
	
	public static void fillInfo(Account a)
	{
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db",null);
		Cursor info = db.query("Accounts", new String[] {"Name", "Password", "Email", "Real_Name"}, "Name='"+a.getUsername()+"'", null, null, null, null, null);
		
		info.moveToFirst();
		if (info.getCount() > 0)
		{
			a.setPassword(info.getString(info.getColumnIndex("Password")));
			a.setEmail(info.getString(info.getColumnIndex("Email")));
			a.setRealname(info.getString(info.getColumnIndex("Real_Name")));
			a.setMasterList(new TaskList(Account.username+"_MasterList"));
		}
		info.close();
		db.close();
	}
	
	public static void deleteAccount(String name)
	{
		SQLiteDatabase db = android.database.sqlite.SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db", null);
		Cursor info = db.query("Accounts", new String[] {"Name"}, "Name='"+ name +"'", null, null, null, null, null);
		if (info.getCount()>0)
		{
			db.execSQL("DELETE FROM Accounts WHERE Name=\"" + name + "\"");
		}
		info.close();
		db.close();
	}
	
	public static boolean checkAccountExists(String name)
	{
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db",null);
		Cursor nameCursor = db.query("Accounts",new String[] {"Name"},"Name=\""+name+"\"", null, null, null, null);
		boolean correct = nameCursor.getCount() > 0;
		nameCursor.close();
		db.close();
		return correct;
	}

	public static boolean checkCorrectPassword(String name, String pass)
	{
		SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.cs2340.taskplusplus/databases/data.db",null);
		Cursor nameCursor = db.query("Accounts",new String[] {"Name"},"Name=\""+name+"\" AND Password=\"" + pass + "\"", null, null, null, null);
		boolean correct = nameCursor.getCount() > 0;
		nameCursor.close();
		db.close();
		return correct;
	}
}